<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript">
		/*获取下级数据*/
		function down(obj) {
			var parentId = ($(obj).val());
			$.ajax({
				url: "/itemCat/getDownByPid",
				data:{parentId:parentId},
				type: "post",
				async: false,//同步
				dataType: "json",
				success: function (result) {
					var itemCatDownList = result.data;
					var table = '';

					$.each(itemCatDownList,function (i,v) {
						table += '<input type="hidden" value="'+ v.parentId +'" name="parentId"><tr><td>';
						table += '<input value="'+ v.id +'" name="box" type="checkbox" ></td>';
						table += '<td>'+ v.id +'</td><td>'+ v.name +'</td><td>'+ v.typeId +'</td><td class="text-center">';
						table += '<button type="button" value="'+ v.id +'" onclick="down(this)" class="btn bg-olive btn-xs" >查询下级</button>';
						table += '<button type="button" value="'+ v.id +'" onclick="toUpdate(this)"  class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>';
						table += '</td></tr>';
					})
					$("#selectDown").html(table);
					head(itemCatDownList[0].parentId);
				},
				error: function () {
					alert.error("系统错误，请联系管理员！");
				}
			})
		}

		//获取头部
		function head(pid) {

			$.ajax({
				url:"/itemCat/head",
				type:"post",
				datatype:"json",
				data:{id:pid},
				async:true,
				success:function(result){
					var up="";
					var up1="";
					var list = result.data;
					up+='<li><a href="javascript:window.location.reload();" >顶级分类列表</a></li>';
					for(var i=list.length;i>0;i--){
						up+='<li><a href="javascript:xiaJi('+list[i-1].parentId+')" >'+list[i-1].name+'</a></li>';
						up1+=''+list[i-1].name+'';
					}
					$("#lol").html(up);
					$("#up").html(up1);
					$("[name=up]").html(up1);
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}
		//获取回显操作
		function toUpdate(obj) {
			var id = ($(obj).val());
			$.ajax({
				url: "/itemCat/getItemCatById",
				data:{id:id},
				type: "post",
				async: false,//同步
				dataType: "json",
				success: function (result) {
					var itemCat = result.data;
					$("#id").val(itemCat.id);
					$("#name").val(itemCat.name);
					$("#updateTypeId").val(itemCat.typeId);
				},
				error: function () {
					alert.error("系统错误，请联系管理员！");
				}
			})
		}
		//修改操作
		function updateItemCat() {
			var id = $("#id").val();
			var name = $("#name").val();
			var typeId = $("#updateTypeId").val();
			$.ajax({
				url: "/itemCat/updateItemCatById",
				data:{id:id,name:name,typeId:typeId},
				type: "post",
				async: false,//同步
				dataType: "json",
				success: function (result) {
					alert(result.message);
					window.location.reload();

				},
				error: function () {
					alert.error("系统错误，请联系管理员！");
				}
			})
		}
		//增加操作
		function addItemCat() {
			var typeId = $("#addTypeId").val();
			var name = $("#addName").val();
			var parentId = $("[name=parentId]").val();
			if (parentId == undefined){
				parentId = 0;
			}
			$.ajax({
				url:"/itemCat/addItemCat",
				data:{typeId:typeId,name:name,parentId:parentId},
				type:"post",
				dataType:"json",
				async:true,
				success:function(result){
					alert(result.message);
					window.location.reload();
				},
				error:function(){
					alert("删除失败");
				}
			})
		}
		/* 删除数据 */
		function deleteById() {
			var boxs = $("input[name=box]:checked");
			var ids = [];
			$(boxs).each(function (i,e) {
				alert($(e).val());
				ids.push($(e).val());
			})
			if (ids.length<=0){
				alert("请选择删除的行");
			}
			if (confirm("确定删除id为"+ ids +"的行？")) {
				$.ajax({
					url:"/itemCat/deleteItemCatByIds",
					data:{ids:ids},
					type:"post",
					dataType:"json",
					async:true,
					success:function(result){

						alert(result.message);
						window.location.reload();
					},
					error:function(){
						alert("删除失败");
					}
				})
			}
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
						<ol class="breadcrumb" th:id="lol">
							<li><a href="javascript:window.location.reload();" >顶级分类列表</a></li>
						</ol>
					</div>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" th:onclick="tou()" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" th:onclick="deleteById()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" onclick="window.location.reload();" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
<!-------------------------------------------------查询获取到的一级数据----------------------------------------------->
			                      <tbody th:id="selectDown">
								  	<th:block th:each="itemCat : ${itemCatList}">
										<input type="hidden" th:value="${itemCat.parentId}" name="addPrentId">
										<tr >
											<td><input th:value="${itemCat.id}" name="box" type="checkbox" ></td>
											<td th:text="${itemCat.id}"></td>
											<td th:text="${itemCat.name}"></td>
											<td th:text="${itemCat.typeId}"></td>
											<td class="text-center">
												<button type="button" th:value="${itemCat.id}" th:onclick="down(this)" class="btn bg-olive btn-xs" >查询下级</button>
												<button type="button" th:value="${itemCat.id}" th:onclick="toUpdate(this)"  class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>
											</td>
										</tr>
									</th:block>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 添加编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
					<td>上级商品分类</td>
					<td th:id="up">顶级分类列表
					</td>
				</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" th:id="addName" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
					<td>
						<select  class="form-control" id="addTypeId">
							<option value="-1">请选择</option>
							<option th:each="type : ${typeTemplateList}" th:value="${type.id}" th:text="${type.name}"></option>
						</select>
					</td>
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" th:onclick="addItemCat()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>


  <!-- 修改编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="ModalLabel">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td name="up">顶级分类列表
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <input type="hidden" th:id="id">
						  <td><input  class="form-control" th:id="name" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select  class="form-control" id="updateTypeId">
								  <option value="-1">请选择</option>
								  <option th:each="type : ${typeTemplateList}" th:value="${type.id}" th:text="${type.name}"></option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" th:onclick="updateItemCat()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>